﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 沙驰评审会打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 沙驰评审会打印()
        {
            InitializeComponent();
        }

        private void 沙驰评审会打印_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
            //绑定品名下拉框
            multiComboBox1.CheckedListBox.Items.Add("全部");
            string sqlpm = "select distinct b.productname from [results] a left join[products] b on a.dm = b.duanma";
            DataTable dtpm = DBHelper2.ExecuteDataTable(sqlpm);
            for (int i = 0; i < dtpm.Rows.Count; i++)
            {
                multiComboBox1.CheckedListBox.Items.Add(dtpm.Rows[i]["productname"].ToString());
            }
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            List<String> combox = multiComboBox1.SelectedItems1;
            if (combox != null)
            {
                if (combox.Count > 0)
                {
                    for (int i = 0; i < combox.Count; i++)
                    {
                        string pm = combox[i].ToString();
                        string sqlpm = "";
                        if (pm == "全部")
                        {
                            sqlpm = @"select distinct a.dm from [results] a left join[products] b on a.dm = b.duanma";
                        }
                        else
                        {
                            sqlpm = @"select distinct a.dm from [results] a left join[products] b on a.dm = b.duanma
                                           where b.productname='{0}'";
                            sqlpm = string.Format(sqlpm, pm);
                        }

                        DataTable dtpm = DBHelper2.ExecuteDataTable(sqlpm);
                        for (int j = 0; j < dtpm.Rows.Count; j++)
                        {
                            comboBox1.Items.Add(dtpm.Rows[j]["dm"].ToString());
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请先选择品名");
                }
            }
            else
            {
                MessageBox.Show("请先选择品名");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dm = comboBox1.Text.ToString();
            //如果单个短码没值就根据品名打
            if (dm == "")
            {
                List<String> combox = multiComboBox1.SelectedItems1;
                if (combox != null)
                {
                    for (int i = 0; i < combox.Count; i++)
                    {
                        string sqlpm = "";
                        string pm = combox[i].ToString();
                        if (pm == "全部")
                        {
                            sqlpm = @"select distinct a.dm from [results] a left join [products] b on a.dm = b.duanma";
                        }
                        else
                        {
                            sqlpm = @"select distinct a.dm from [results] a left join [products] b on a.dm = b.duanma
                                      where b.productname='{0}' order by a.dm";
                            sqlpm = string.Format(sqlpm, pm);
                        }
                        DataTable dtpm = DBHelper2.ExecuteDataTable(sqlpm);
                        for (int j = 0; j < dtpm.Rows.Count; j++)
                        {
                            string sqldm = @"exec zdy_getInfoByDm '{0}'";
                            sqldm = string.Format(sqldm, dtpm.Rows[j]["dm"].ToString());
                            DataTable dtdm = DBHelper2.ExecuteDataTable(sqldm);

                            NewUsb.FindUSBPrinter();
                            for (int k = 0; k < NewUsb.USBPortCount; k++)
                            {
                                if (NewUsb.LinkUSB(k))
                                {
                                    SendData2USB("SIZE 40 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                                    SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                                    SendData2USB("CLS\r\n");//清空缓冲区
                                    SendData2USB("DENSITY 7\r\n");//打印浓度
                                    SendData2USB("REFERENCE 0,0\r\n");
                                    SendData2USB("TEXT 10,10,\"TSS16.BF2\",0,1,1,\"短码\"\r\n");
                                    SendData2USB("TEXT 50,10,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["dm"].ToString() + "\"\r\n");
                                    SendData2USB("TEXT 100,10,\"TSS16.BF2\",0,1,1,\"品名\"\r\n");
                                    SendData2USB("TEXT 150,10,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["productname"].ToString() + "\"\r\n");
                                    SendData2USB("TEXT 10,40,\"TSS16.BF2\",0,1,1,\"颜色\"\r\n");
                                    SendData2USB("TEXT 50,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["color"].ToString() + "\"\r\n");
                                    SendData2USB("TEXT 100,40,\"TSS16.BF2\",0,1,1,\"价格\"\r\n");
                                    SendData2USB("TEXT 150,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["price"].ToString() + "\"\r\n");
                                    SendData2USB("TEXT 200,40,\"TSS16.BF2\",0,1,1,\"系列\"\r\n");
                                    SendData2USB("TEXT 240,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["xilie"].ToString() + "\"\r\n");
                                    int gd = 40;
                                    for (int x = 0; x < dtdm.Rows.Count; x++)
                                    {
                                        gd = gd + 30;
                                        SendData2USB("TEXT 10," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[x]["username"].ToString() + "\"\r\n");
                                        SendData2USB("TEXT 80," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[x]["pf"].ToString() + "\"\r\n");
                                    }
                                    string sqldmpjf = @"exec [zdy_getInfoByDm_pjf] '{0}'";
                                    sqldmpjf = string.Format(sqldmpjf, dtpm.Rows[j]["dm"].ToString());
                                    DataTable dtdmpjf = DBHelper2.ExecuteDataTable(sqldmpjf);
                                    gd = gd + 30;
                                    SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"平均分\"\r\n");
                                    SendData2USB("TEXT 100," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["pjf"].ToString() + "\"\r\n");

                                    SendData2USB("PRINT 1\r\n");
                                    NewUsb.CloseUSBPort();
                                }
                                else
                                {
                                    MessageBox.Show("请确定是否连接上打印机");
                                }
                            }

                        }
                    }
                }
            }
            else
            {
                string sqldm = @"exec zdy_getInfoByDm '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtdm = DBHelper2.ExecuteDataTable(sqldm);
                NewUsb.FindUSBPrinter();
                for (int k = 0; k < NewUsb.USBPortCount; k++)
                {
                    if (NewUsb.LinkUSB(k))
                    {
                        SendData2USB("SIZE 40 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                        SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                        SendData2USB("CLS\r\n");//清空缓冲区
                        SendData2USB("DENSITY 7\r\n");//打印浓度
                        SendData2USB("REFERENCE 0,0\r\n");
                        SendData2USB("TEXT 10,10,\"TSS16.BF2\",0,1,1,\"短码\"\r\n");
                        SendData2USB("TEXT 50,10,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["dm"].ToString() + "\"\r\n");
                        SendData2USB("TEXT 100,10,\"TSS16.BF2\",0,1,1,\"品名\"\r\n");
                        SendData2USB("TEXT 150,10,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["productname"].ToString() + "\"\r\n");
                        SendData2USB("TEXT 10,40,\"TSS16.BF2\",0,1,1,\"颜色\"\r\n");
                        SendData2USB("TEXT 50,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["color"].ToString() + "\"\r\n");
                        SendData2USB("TEXT 100,40,\"TSS16.BF2\",0,1,1,\"价格\"\r\n");
                        SendData2USB("TEXT 150,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["price"].ToString() + "\"\r\n");
                        SendData2USB("TEXT 200,40,\"TSS16.BF2\",0,1,1,\"系列\"\r\n");
                        SendData2USB("TEXT 240,40,\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[0]["xilie"].ToString() + "\"\r\n");
                        int gd = 40;
                        for (int x = 0; x < dtdm.Rows.Count; x++)
                        {
                            gd = gd + 30;
                            SendData2USB("TEXT 10," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[x]["username"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 80," + gd + ",\"TSS16.BF2\",0,1,1,\"" + dtdm.Rows[x]["pf"].ToString() + "\"\r\n");
                        }
                        string sqldmpjf = @"exec [zdy_getInfoByDm_pjf] '{0}'";
                        sqldmpjf = string.Format(sqldmpjf, dm);
                        DataTable dtdmpjf = DBHelper2.ExecuteDataTable(sqldmpjf);
                        gd = gd + 30;
                        SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"平均分\"\r\n");
                        SendData2USB("TEXT 100," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["pjf"].ToString() + "\"\r\n");

                        SendData2USB("PRINT 1\r\n");
                        NewUsb.CloseUSBPort();
                    }
                    else
                    {
                        MessageBox.Show("请确定是否连接上打印机");
                    }
                }
            }
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                string sqldm = @"exec zdy_getInfoByDm '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtdm = DBHelper2.ExecuteDataTable(sqldm);
                if (dtdm.Rows.Count > 0)
                {
                    NewUsb.FindUSBPrinter();
                    for (int k = 0; k < NewUsb.USBPortCount; k++)
                    {
                        if (NewUsb.LinkUSB(k))
                        {
                            SendData2USB("SIZE 60 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                            SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                            SendData2USB("CLS\r\n");//清空缓冲区
                            SendData2USB("DENSITY 7\r\n");//打印浓度
                            SendData2USB("REFERENCE 0,0\r\n");

                            SendData2USB("TEXT 10,10,\"TSS24.BF2\",0,1,1,\"品类\"\r\n");
                            SendData2USB("TEXT 100,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["productname"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 270,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["xilie"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 350,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["zhuti"].ToString() + "\"\r\n");
                        
      
                            SendData2USB("TEXT 10,45,\"TSS24.BF2\",0,1,1,\"短码\"\r\n");
                            SendData2USB("TEXT 100,45,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["dm"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,80,\"TSS24.BF2\",0,1,1,\"价格\"\r\n");
                            SendData2USB("TEXT 100,80,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["price"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,80,\"TSS24.BF2\",0,1,1,\"版型\"\r\n");
                            SendData2USB("TEXT 300,80,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["banxing"].ToString() + "\"\r\n");

                           

                            //代理
                            string sqldm_dl = @"exec [zdy_getInfoByDm_dl] '{0}'";
                            sqldm_dl = string.Format(sqldm_dl, dm);
                            DataTable dtdm_dl = DBHelper2.ExecuteDataTable(sqldm_dl);
                            int gd = 85;
                            for (int x = 0; x < dtdm_dl.Rows.Count; x++)
                            {
                                gd = gd + 30;
                                SendData2USB("TEXT 250," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_dl.Rows[x]["username"].ToString() + "\"\r\n");
                                SendData2USB("TEXT 400," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_dl.Rows[x]["pf"].ToString() + "\"\r\n");
                            }

                            //直联营
                            string sqldm_zly = @"exec [zdy_getInfoByDm_zly] '{0}'";
                            sqldm_zly = string.Format(sqldm_zly, dm);
                            DataTable dtdm_zly = DBHelper2.ExecuteDataTable(sqldm_zly);
                            gd = 85;
                            for (int x = 0; x < dtdm_zly.Rows.Count; x++)
                            {
                                gd = gd + 30;
                                SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_zly.Rows[x]["username"].ToString() + "\"\r\n");
                                SendData2USB("TEXT 160," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_zly.Rows[x]["pf"].ToString() + "\"\r\n");
                            }

                            string sqldmpjf = @"exec [zdy_getsum] '{0}'";
                            sqldmpjf = string.Format(sqldmpjf, dm);
                            DataTable dtdmpjf = DBHelper2.ExecuteDataTable(sqldmpjf);
                            gd = gd + 35;
                            SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"直联营\"\r\n");
                            SendData2USB("TEXT 160," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["zlypjf"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 250," + gd + ",\"TSS24.BF2\",0,1,1,\"代理\"\r\n");
                            SendData2USB("TEXT 350," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["dlpjf"].ToString() + "\"\r\n");
                            gd = gd + 35;
                            SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"单款平均分\"\r\n");
                            SendData2USB("TEXT 250," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["dkpjf"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 350," + gd + ",\"TSS24.BF2\",0,1,1,\"排名\"\r\n");
                            gd = gd + 35;
                            SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"品类平均\"\r\n");
                            SendData2USB("TEXT 250," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["plpjf"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 350," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["plpm"].ToString() + "\"\r\n");
                            gd = gd + 35;
                            SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"品类价位段平均\"\r\n");
                            SendData2USB("TEXT 250," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["pljwdpjf"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 350," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[0]["pljwdpm"].ToString() + "\"\r\n");


                            SendData2USB("PRINT 1\r\n");
                            NewUsb.CloseUSBPort();

                            textBox1.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("请确定是否连接上打印机");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("短码不正确");
                }
            }

        }
    }
}
